package io.xccit.hotelsystem.ui.order.adapter;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import io.xccit.hotelsystem.R;

/**
 * 订单适配器
 */
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.OrderViewHolder> {

    public interface OnItemClickListener {
        void onItemClick(int orderId);
    }

    private Context context;
    private Cursor cursor;
    private OnItemClickListener listener;

    public OrderAdapter(Context context, Cursor cursor, OnItemClickListener listener) {
        this.context = context;
        this.cursor = cursor;
        this.listener = listener;
    }

    @NonNull
    @Override
    public OrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_order, parent, false);
        return new OrderViewHolder(view, this);
    }

    @Override
    public void onBindViewHolder(@NonNull OrderViewHolder holder, int position) {
        if (!cursor.moveToPosition(position)) {
            return;
        }

        String hotelName = cursor.getString(cursor.getColumnIndexOrThrow("hotel_name"));
        String startTime = cursor.getString(cursor.getColumnIndexOrThrow("start_time"));
        String endTime = cursor.getString(cursor.getColumnIndexOrThrow("end_time"));
        int status = cursor.getInt(cursor.getColumnIndexOrThrow("order_status"));
        double price = cursor.getDouble(cursor.getColumnIndexOrThrow("price"));

        holder.tvHotelName.setText(hotelName);
        holder.tvStartTime.setText(startTime);
        holder.tvEndTime.setText(endTime);
        holder.tvPrice.setText(String.format("¥%.2f", price));

        String statusText = "";
        int statusColor = R.color.black;

        switch (status) {
            case 1:
                statusText = "未入住";
                statusColor = R.color.warm_blue_500;
                break;
            case 2:
                statusText = "已完成";
                statusColor = R.color.warm_green_500;
                break;
            case 3:
                statusText = "已退订";
                statusColor = R.color.warm_red_500;
                break;
        }

        holder.tvOrderStatus.setText(statusText);
        holder.tvOrderStatus.setTextColor(context.getResources().getColor(statusColor));
    }

    @Override
    public int getItemCount() {
        return cursor != null ? cursor.getCount() : 0;
    }

    public void swapCursor(Cursor newCursor) {
        if (cursor != null) {
            cursor.close();
        }
        cursor = newCursor;
        if (newCursor != null) {
            notifyDataSetChanged();
        }
    }

    public static class OrderViewHolder extends RecyclerView.ViewHolder {
        TextView tvHotelName, tvOrderStatus, tvStartTime, tvEndTime, tvPrice;
        private OrderAdapter adapter;

        public OrderViewHolder(@NonNull View itemView, OrderAdapter adapter) {
            super(itemView);
            this.adapter = adapter;
            tvHotelName = itemView.findViewById(R.id.tvHotelName);
            tvOrderStatus = itemView.findViewById(R.id.tvOrderStatus);
            tvStartTime = itemView.findViewById(R.id.tvStartTime);
            tvEndTime = itemView.findViewById(R.id.tvEndTime);
            tvPrice = itemView.findViewById(R.id.tvPrice);
            
            itemView.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION && adapter.listener != null) {
                    adapter.cursor.moveToPosition(position);
                    int orderId = adapter.cursor.getInt(adapter.cursor.getColumnIndexOrThrow("id"));
                    adapter.listener.onItemClick(orderId);
                }
            });
        }
    }
}